﻿
using Xacml.Core.Interfaces;
using Xacml.Core.Runtime;

namespace Xacml.Core.Runtime.Functions
{
  public abstract class BaseSetEquals : FunctionBase, ITypeSpecificFunction
  {
    public override IDataType Returns
    {
      get
      {
        return (IDataType) DataTypeDescriptor.Boolean;
      }
    }

    public override IDataType[] Arguments
    {
      get
      {
        return new IDataType[2]
        {
          (IDataType) DataTypeDescriptor.Bag,
          (IDataType) DataTypeDescriptor.Bag
        };
      }
    }

    public abstract IDataType DataType { get; }

    public override EvaluationValue Evaluate(EvaluationContext context, params IFunctionParameter[] args)
    {
      IFunction subsetFunction = this.DataType.SubsetFunction;
      if (subsetFunction.Evaluate(context, (IFunctionParameter) new EvaluationValue((object) args[0], args[0].GetType(context)), (IFunctionParameter) new EvaluationValue((object) args[1], args[1].GetType(context))) == EvaluationValue.True)
      {
        if (subsetFunction.Evaluate(context, (IFunctionParameter) new EvaluationValue((object) args[1], args[1].GetType(context)), (IFunctionParameter) new EvaluationValue((object) args[0], args[0].GetType(context))) == EvaluationValue.True)
          return EvaluationValue.True;
      }
      return EvaluationValue.False;
    }
  }
}
